home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / GraphicViewers / Viewer_Yacktman / Source / ImageView.m < prev    next >
Text File  |  1991-10-07  |  3KB  |  112 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. #import "ImageView.h"
  5.  
  6. @implementation ImageView
  7.  
  8. - initFrame:(const NXRect *)frm            // initialize instance
  9. {
  10.     [super initFrame:frm];
  11.     origin.x = 0;
  12.     origin.y = 0;
  13.     xsize = 0;
  14.     ysize = 0;
  15.     return self;
  16. }
  17.  
  18. -(int)tag
  19. {
  20.     return 69;
  21. }
  22.  
  23. - (BOOL)loadFile:(const char *)name x:(int)winX y:(int)winY    // load pic
  24. {
  25.     id imageRep;
  26.     int file, numread, i;
  27.     unsigned long amount, rdt, ys, xs;
  28.     unsigned char lengths[16];
  29.     unsigned char *dataArray;
  30.  
  31.     file = open(name, O_RDONLY, 0666);
  32.     if (!file) { printf("File open error...\n"); return NO;}
  33.    
  34.     numread = read(file, lengths, 8);
  35.     if (numread != 8) {printf("Read error...aborting.\n"); return NO;}
  36.     xsize = 0; ysize = 0;
  37.     for (i=0; i<4; i++) {
  38.         xsize *= 256;
  39.         xsize += lengths[i];
  40.     }
  41.     for (i=0; i<4; i++) {
  42.         ysize *= 256;
  43.         ysize += lengths[i];
  44.     }
  45.     
  46.     imageRep = [[NXBitmapImageRep alloc] initData:NULL
  47.             pixelsWide:xsize pixelsHigh:ysize
  48.         bitsPerSample:8 samplesPerPixel:3
  49.         hasAlpha:NO isPlanar:NO colorSpace:NX_RGBColorSpace
  50.         bytesPerRow:xsize*3 bitsPerPixel:0];
  51.     if (imageRep == nil) { printf("Couldn't create ImageRep."); return NO; }
  52.     dataArray = [imageRep data];
  53.     
  54.     amount = xsize*ysize*3;
  55.     if ((rdt = read(file, dataArray++, amount)) != amount) {
  56.      printf("Reading data error.\n");
  57.     }
  58.  
  59.     if (close(file)) { printf("File close error...\n"); }
  60.     ys = ((ysize > 512) ? 512 : ysize);
  61.     xs = ((xsize > 512) ? 512 : xsize);
  62.     [self sizeTo:xsize :ysize];
  63.     [[self window] sizeWindow:(xs+19) :(ys+19)];
  64.     [[self window] moveTo:(winX) :(winY-ys+129)];
  65.     image = [[NXImage alloc] initSize:&bounds.size];
  66.     if (NO == [image useRepresentation:imageRep]) {
  67.         printf("Couldn't use imageRep.\n");
  68.     return NO;
  69.     }
  70.     [self update];
  71.     [[self window] makeKeyAndOrderFront:self];
  72.     return YES;
  73. }
  74.  
  75. - (BOOL)saveFile:(const char *)name     // save pic
  76. {
  77.     int fd;
  78.     NXStream *stream;
  79.     
  80.     fd = open(name, O_CREAT | O_WRONLY | O_TRUNC, 0666);
  81.     if (!fd) { printf("Error opening save file %s.\n", name); }
  82.     stream = NXOpenFile(fd, NX_WRITEONLY);
  83.     if (stream == NULL) { printf("Error opening save stream.\n"); }
  84.     [image writeTIFF:stream];
  85.     NXFlush(stream);
  86.     NXClose(stream);
  87.     close(fd);
  88.     return YES;
  89. }
  90.  
  91. - drawSelf:(NXRect *)rects :(int)rectCount      // standard rendering method
  92. {
  93.     [self lockFocus];
  94.     PSsetgray(NX_BLACK);
  95.     NXRectFill(&bounds);
  96.     [image composite:NX_COPY toPoint:&origin];
  97.     [[self window] flushWindow];
  98.     [self unlockFocus];
  99.     return self;
  100. }
  101.  
  102. - windowWillResize:sender toSize:(NXSize *)newFrame    // to control max size
  103. {
  104.     if (newFrame->width > (xsize + 21)) newFrame->width = xsize + 21;
  105.     if (newFrame->height > (ysize + 51)) newFrame->height = ysize + 51;
  106.     if (newFrame->width < 64) newFrame->width = 64;
  107.     if (newFrame->height < 118) newFrame->height = 118;
  108.     return self;
  109. }
  110.  
  111. @end
  112.